package service

import (
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"time"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/teacher/service/dto"
	cDto "tuowei.com/common/dto"
)

type StudentsService struct {
	service.Service
}

// 招生老师获取某个订单的详情(以前叫学生详情)
func (s *StudentsService) GetStudentsDetail(c *dto.GetStudentDetailRequest, detail *models.SysStudent) error {
	err := s.Orm.
		Joins("College").Joins("ProvinceRegion").Joins("AreaPoint").
		Scopes(
			cDto.MakeCondition(*c),
		).Table(detail.TableName()).
		First(detail).
		Error
	return err
}

// GetStudentsList
// 获取分班/未分班学生列表(enrollment_teacher_id招生老师id是当前老师)
func (s *StudentsService) GetStudentsList(lists *[]models.SysStudent, count *int64, teacherId int, req dto.GetStudentsListRequest) error {
	db := s.Orm.Where("enrollment_teacher_id = ?", teacherId).Scopes(
		cDto.Paginate(req.GetPageSize(), req.GetPageIndex()),
		models.SysStudent{}.Able(),
	)
	db = db.Where("class_id = ?", 0)
	return db.Find(lists).Count(count).Error
}

func (s *StudentsService) GetOfflineOrderDetailById(detail *models.OfflineOrder) error {
	return s.Orm.Preload("Customer").First(detail).Error
}

func (s *StudentsService) GetStudentByIds(classIds []int) ([]models.SysStudent, error) {
	lists := make([]models.SysStudent, 0)
	return lists, s.Orm.Where("id in (?)", classIds).Find(&lists).Error
}

func (s *StudentsService) DivisionStudent(c *dto.DivisionStudentRequest) error {
	return s.Orm.Where("id in (?)", c.Ids).Updates(models.SysStudent{
		ClassId: c.ClassId,
	}).Error
}

func (s *StudentsService) GetClassByAreaId(c *dto.GetClassRequest, classes *[]models.SysClass) error {
	return s.Orm.Where("dept_id = ?", c.AreaId).Find(classes).Error
}

func (s *StudentsService) GetManageStudentsLists(teacherId int, c *dto.GetManageStudentsListsRequest) ([]models.SysStudent, int64, error) {
	tx := s.Orm.Where("enrollment_teacher_id = ?", teacherId).
		Preload("WxStudents").Preload("QqStudents").
		Scopes(cDto.Paginate(c.GetPageSize(), c.GetPageIndex())).Preload("StateInfo")
	var count int64
	if c.Area > 0 {
		tx = tx.Where("area = ?", c.Area)
	}
	if c.State > 0 {
		tx = tx.Where("state = ?", c.State)
	}
	if c.Label != "" {
		tx = tx.Where("user_type = ?", c.Label)
	}
	if c.Name != "" {
		tx = tx.Where("name = ?", c.Name)
	}
	if c.Divided != nil {
		if *c.Divided == true {
			tx = tx.Where("class_id > 0")
		} else {
			tx = tx.Where("class_id = 0")
		}
	}
	rs := make([]models.SysStudent, 0)
	return rs, count, tx.Find(&rs).Count(&count).Error
}

// 获取邀请学员的数量和消费总金额
func (s *StudentsService) GetInvitedStudentsNums(teacherId int) (int, float64) {
	var count int64
	s.Orm.Table(models.SysStudent{}.TableName()).Where("teach_recommender = ?", teacherId).Count(&count)
	var amount float64
	s.Orm.Table(models.SysStudent{}.TableName()).
		//Where("teach_recommender = ?", teacherId).
		Or("enrollment_teacher_id = ?", teacherId).
		Select("sum(total_pay) as amount").Scan(&amount)
	return int(count), amount
}

func (s *StudentsService) StatisticsByTime(startTime time.Time, teacherId int) []*models.SysStudent {
	lists := make([]*models.SysStudent, 0)
	s.Orm.Table(models.SysStudent{}.TableName()).
		Where("enrollment_teacher_id = ?", teacherId).
		Where("created_at > ?", startTime).
		Select("id,created_at,ke_bie").Find(&lists)
	return lists
}

// 本月邀请学员数
func (s *StudentsService) InvitedStudentsNum(year int, month time.Month, teacherId int) int {
	var count int64
	s.Orm.Table(models.SysStudent{}.TableName()).
		Where("teach_recommender = ?", teacherId).
		Where("created_at > ?", time.Date(year, month, 1, 0, 0, 0, 0, time.Local)).
		Count(&count)
	return int(count)
}

// 总共邀请学员数
func (s *StudentsService) InvitedTotalStudentsNum(teacherId int) int {
	var count int64
	s.Orm.Table(models.SysStudent{}.TableName()).
		Where("teach_recommender = ?", teacherId).Count(&count)
	return int(count)
}

// 总共邀请学员数
func (s *StudentsService) InvitedTotalPayedStudentsNum(teacherId int) int {
	var count int64
	s.Orm.Table(models.SysStudent{}.TableName()).
		Where("teach_recommender = ?", teacherId).
		Where("total_pay > 0").
		Count(&count)
	return int(count)
}
